﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 图书管理系统
{

    public partial class 预约 : Form
    {
        public 预约()
        {
            InitializeComponent();
        }
        public books books;
        public int index;
        public int method;
        public String content;
        public 预约(String name,String tel,String val_count,String isbn,String bookname,String inline,String BookNum,books books,int index, int method, String content)
        {
            InitializeComponent();
            label4.Text = name;
            label5.Text = tel;
            label6.Text = val_count;
            label11.Text = isbn;
            label10.Text = bookname;
            label12.Text = inline;
            label13.Text = BookNum;
            this.books = books;
            this.index = index;
            this.method = method;
            this.content = content;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            //书籍详情 form = new 书籍详情(books);
            
            this.Close();
            //form.Show();
        }

        private void 预约_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //判断是否预约数已满
            int booknum = Convert.ToInt32(label6.Text);
            if (booknum == 0)
            {
                MessageBox.Show("最大预约数量已满，不能再借阅");
            }
            else
            {
                User user = 用户登录.user;
                SqlConnection conn = new SqlConnection();
                SqlConnection conn1 = new SqlConnection();
                SqlConnection conn2 = new SqlConnection();
                SqlConnection conn3 = new SqlConnection();
                SqlConnection conn4 = new SqlConnection();
                String id = user.Id;
                SqlConnection conn5 = new SqlConnection();


                try
                {
                    int flag = 1;
                    //查询是否已预约过这本书
                    string sql = "select * from Appointmentrecord_Table where BookNum='" + label13.Text + "'and OrderState ='预约中' and ReaderID='" + id.ToString() + "'";
                    SqlCommand cmd = Dao.SqlConnect(sql, conn);
                    conn.Open();
                    SqlDataReader sdr = cmd.ExecuteReader();

                    if (sdr.HasRows)
                    {
                        MessageBox.Show("您已经预约过这本书了");
                        flag = 0;

                    }
                    //查询是否借过这本书
                    
                    string sql3 = "select * from BorrowinfoTable where BorrowState ='借阅中' and ReaderID='" + id.ToString() + "'and BookNum='"+label13.Text+"'";
                    SqlCommand cmd3 = Dao.SqlConnect(sql3, conn3);
                    conn3.Open();
                    SqlDataReader sdr3 = cmd3.ExecuteReader();

                    if (sdr3.HasRows)
                    {
                        MessageBox.Show("您正在借阅这本书");
                        flag = 0;

                    }
                    //查询是否有欠费                   
                    string sql1 = "select * from Penaltyrecord_Table where FineState='未缴费' and FineStyle='逾期费' and ReaderID='" + id.ToString() + "'";
                    SqlCommand cmd1 = Dao.SqlConnect(sql1, conn1);
                    conn1.Open();
                    SqlDataReader sdr1 = cmd1.ExecuteReader();
                    if (sdr1.HasRows)
                    {
                        MessageBox.Show("您还有欠费记录,请先缴纳欠费");
                        flag = 0;
                    }
                    //前方没人排队，写入提示信息

                    int tips = Convert.ToInt32(label12.Text);

                    if (tips == 0)
                    {
                        SqlConnection conn7 = new SqlConnection();
                        String sql7 = "select OrderTerm from Borrowingrules_Table where Identy = '" + user.Identy + "'";
                        SqlCommand cmd7 = Dao.SqlConnect(sql7, conn7);
                        conn7.Open();
                        SqlDataReader th2 = cmd7.ExecuteReader();
                        String tr2 = null;
                        while (th2.Read())
                        {
                            tr2 = th2["OrderTerm"].ToString();
                        }
                        int n = Convert.ToInt32(tr2);
                        DateTime Tiptime = DateTime.Now;
                        String Tiptime1 = Tiptime.AddDays(+n).ToString();
                        String tips1 = "请于" + Tiptime1 + "前取书";
                        conn7.Close();

                        String s = DateTime.Now.ToLocalTime().ToString();
                        string sql4 = "insert into Appointmentrecord_Table(ReaderID,ISBN,Order_time,OrderState,BookNum,Tips) values(@ReaderID,@ISBN,@Order_time,@OrderState,@BookNum,@Tips)";
                        SqlCommand cmd4 = Dao.SqlConnect(sql4, conn4);
                        conn4.Open();
                        cmd4.Parameters.Add("@ReaderID", SqlDbType.Char, 15).Value = id;
                        cmd4.Parameters.Add("@ISBN", SqlDbType.VarChar, 30).Value = label11.Text;
                        cmd4.Parameters.Add("@Order_time", SqlDbType.DateTime).Value = s;
                        cmd4.Parameters.Add("@OrderState", SqlDbType.Char, 30).Value = "预约中";
                        cmd4.Parameters.Add("@BookNum", SqlDbType.Char, 20).Value = label13.Text;
                        cmd4.Parameters.Add("@Tips", SqlDbType.VarChar, 100).Value = tips1;
                        int i = cmd4.ExecuteNonQuery();
                        //更改图书状态

                        string sql5 = "update Binfo_Table set BookStatus='不可借' where BookNum='" + label13.Text + "'";
                        SqlCommand cmd5 = Dao.SqlConnect(sql5, conn5);
                        conn5.Open();
                        cmd5.ExecuteNonQuery();
                        conn5.Close();

                        flag = 0;
                        MessageBox.Show("预约成功！");
                        MessageBox.Show(tips1);
                        books.GetBooks1[index].BookStatus1 = "不可借";

                    }
                    if (flag == 1)
                    {//添加预约记录
                        String s = DateTime.Now.ToLocalTime().ToString();
                        string sql2 = "insert into Appointmentrecord_Table(ReaderID,ISBN,Order_time,OrderState,BookNum) values(@ReaderID,@ISBN,@Order_time,@OrderState,@BookNum)";
                        SqlCommand cmd2 = Dao.SqlConnect(sql2, conn2);
                        conn2.Open();
                        cmd2.Parameters.Add("@ReaderID", SqlDbType.Char, 15).Value = id;
                        cmd2.Parameters.Add("@ISBN", SqlDbType.VarChar, 30).Value = label11.Text;
                        cmd2.Parameters.Add("@Order_time", SqlDbType.DateTime).Value = s;
                        cmd2.Parameters.Add("@OrderState", SqlDbType.Char, 30).Value = "预约中";
                        cmd2.Parameters.Add("@BookNum", SqlDbType.Char, 20).Value = label13.Text;
                        int i = cmd2.ExecuteNonQuery();
                        MessageBox.Show("预约成功！！");
                        conn2.Close();
                        //更改图书状态
                        string sql5 = "update Binfo_Table set BookStatus='不可借' where BookNum='" + label13.Text + "'";
                        SqlCommand cmd5 = Dao.SqlConnect(sql5, conn5);
                        conn5.Open();
                        cmd5.ExecuteNonQuery();
                        conn5.Close();
                        books.GetBooks1[index].BookStatus1 = "不可借";
                    }
                    
                    


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                    conn1.Close();
                    conn2.Close();
                    conn3.Close();
                    conn4.Close();
                }
            }
            书籍详情 form = new 书籍详情(books, method, content);
            form.Show();
            this.Close();
           
        }
    }
}
